package frontend;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dbmanager.AccountManager;
import dbmanager.DatabaseManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CreateUserGUI extends javax.swing.JFrame {
	private LoginGUI parentFrame;
	private JLabel fullnameLabel;
	private JTextField fullnameTextField;
	private JLabel passwordLabel1;
	private JLabel errorLabel;
	private JLabel filloutfieldsLabel;
	private JLabel createLabel;
	private JButton registerButton;
	private JPasswordField passwordField2;
	private JPasswordField passwordField1;
	private JTextField usernameField;
	private JTextField emailAddressField;
	private JLabel passwordLabel2;
	private JLabel usernameLabel;
	private JLabel emailAddressLabel;
	private AccountManager acm;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	
	public CreateUserGUI(LoginGUI parent) {
		super();
		acm = new AccountManager();
		initGUI();
		this.parentFrame = parent;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			this.setTitle("Creating a new NOM User Account");
			this.setResizable(false);
			getContentPane().setLayout(null);
			this.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent evt) {
					thisWindowClosed(evt);
				}
			});
			{
				fullnameLabel = new JLabel();
				getContentPane().add(fullnameLabel);
				fullnameLabel.setText("Full Name");
				fullnameLabel.setBounds(23, 65, 75, 22);
				fullnameLabel.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				fullnameTextField = new JTextField();
				getContentPane().add(fullnameTextField);
				fullnameTextField.setBounds(171, 61, 359, 25);
			}
			{
				emailAddressLabel = new JLabel();
				getContentPane().add(emailAddressLabel);
				emailAddressLabel.setBounds(23, 102, 110, 25);
				emailAddressLabel.setText("Email Address");
				emailAddressLabel.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				usernameLabel = new JLabel();
				getContentPane().add(usernameLabel);
				usernameLabel.setText("Desired Username");
				usernameLabel.setBounds(23, 165, 131, 18);
				usernameLabel.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				passwordLabel1 = new JLabel();
				getContentPane().add(passwordLabel1);
				passwordLabel1.setText("Password");
				passwordLabel1.setBounds(23, 202, 75, 16);
				passwordLabel1.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				passwordLabel2 = new JLabel();
				getContentPane().add(passwordLabel2);
				passwordLabel2.setText("Verify Password");
				passwordLabel2.setBounds(23, 239, 110, 21);
				passwordLabel2.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				emailAddressField = new JTextField();
				getContentPane().add(emailAddressField);
				emailAddressField.setBounds(171, 98, 359, 25);
			}
			{
				usernameField = new JTextField();
				getContentPane().add(usernameField);
				usernameField.setBounds(171, 161, 359, 25);
			}
			{
				passwordField1 = new JPasswordField();
				getContentPane().add(passwordField1);
				passwordField1.setBounds(171, 198, 359, 25);
			}
			{
				passwordField2 = new JPasswordField();
				getContentPane().add(passwordField2);
				passwordField2.setBounds(171, 235, 359, 25);
			}
			{
				registerButton = new JButton();
				getContentPane().add(registerButton);
				registerButton.setText("Sign me up!");
				registerButton.setBounds(386, 286, 144, 29);
				registerButton.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							registerButtonMouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				createLabel = new JLabel();
				getContentPane().add(createLabel);
				createLabel.setText("Creating a NOM User Account");
				createLabel.setBounds(23, 12, 306, 24);
				createLabel.setFont(new java.awt.Font("AlArabiya",1,18));
			}
			{
				filloutfieldsLabel = new JLabel();
				getContentPane().add(filloutfieldsLabel);
				filloutfieldsLabel.setText("Fill out the following fields to create your NOM User Account.");
				filloutfieldsLabel.setBounds(23, 35, 384, 15);
			}
			{
				errorLabel = new JLabel();
				getContentPane().add(errorLabel);
				errorLabel.setFont(new java.awt.Font("AlArabiya",1,13));
				errorLabel.setBounds(23, 286, 351, 25);
			}
			pack();
			this.setSize(559, 359);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void registerButtonMouseClicked(MouseEvent evt) throws SQLException {
		
		String pw1 = new String(passwordField1.getPassword());
		String pw2 = new String(passwordField2.getPassword());
		String fullname = fullnameTextField.getText();
		String username = usernameField.getText();
		String email = emailAddressField.getText();
		
		// Make sure they filled everything out
		if(fullname.equals("") || pw1.equals("") || pw2.equals("") || username.equals("") || email.equals("")){
			errorLabel.setText("Please fill out all of the fields!");
			return;
		}
		// Check that the passwords are the same.
		if(!pw1.equals(pw2)){
			errorLabel.setText("Passwords do not match!");
			return;
		}
		
		// Check whether the username is in use.
		if(AccountManager.userExists(username)){
			errorLabel.setText("We're sorry, that username is aleady in use!");
			return;
		
		}
		// We're good! Add the username!
		if(!AccountManager.createUser(username, pw1, email, fullname)){
			errorLabel.setText("Error while creating your user account.");
			return;
		}
		parentFrame.setUsername(username);
		this.dispose();
		JOptionPane.showMessageDialog(this, "You've successfully created a new NOM User Account!");

		
	}
	
	private void thisWindowClosed(WindowEvent evt) {
		this.parentFrame.setLoginGUIOpen(false);
	}

}
